<template>
  <div>
    <div @click="change_tab" class="tab-btn">
      <p name="personal" :class="{ active: name == 'personal' }">个人</p>
      <p name="enterprise" :class="{ active: name == 'enterprise' }">企业</p>
    </div>
    <el-form
      v-if="name == 'personal'"
      ref="form_reg_personal"
      :model="form_reg_personal"
    >
      <el-form-item label="手机">
        <el-input v-model="form_reg_personal.phone"></el-input>
      </el-form-item>
      <el-form-item>
        <el-row :gutter="20">
          <el-col :span="18">
            <el-input
              placeholder="请输入验证码"
              v-model="form_reg_personal.code"
            ></el-input>
          </el-col>
          <el-col :span="6">
            <el-button>获取验证码</el-button>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="昵称">
        <el-input v-model="form_reg_personal.name"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form_reg_personal.pwd"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button>注册</el-button>
      </el-form-item>
    </el-form>
    <el-form v-else :model="form_reg_enterprise">
      <el-form-item label="企业名称">
        <el-input>
          <el-input></el-input>
        </el-input>
      </el-form-item>
      <el-form-item label="纳税识别号">
        <el-input>
          <el-input></el-input>
        </el-input>
      </el-form-item>
      <el-form-item label="营业执照副本">
        <el-upload
          class="avatar-uploader"
          action="http://back.turawstock.com/api/login/register"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="手机号码">
        <el-input>
          <el-input></el-input>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-row :gutter="20">
          <el-col :span="18">
            <el-input
              placeholder="请输入验证码"
              v-model="form_reg_personal.code"
            ></el-input>
          </el-col>
          <el-col :span="6">
            <el-button>获取验证码</el-button>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="昵称">
        <el-input v-model="form_reg_personal.name"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form_reg_personal.pwd"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button>注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      name: "personal",
      form_reg_personal: {
        phone: "",
        code: "",
        name: "",
        pwd: "",
      },
      form_reg_enterprise: {},
      imageUrl: "",
    };
  },
  methods: {
    change_tab(e) {
      this.name = e.target.getAttribute("name");
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>

<style lang="scss" scoped>
.tab-btn {
  margin: 20px 0;
  display: flex;
  p {
    flex: 1;
    text-align: center;
    border: 1px solid #dbe0df;
    font-size: 16px;
    height: 48px;
    line-height: 48px;
    cursor: pointer;
    &.active {
      background-color: #f4f7f6;
    }
  }
}
</style>
